package com.qls.web.modules.op.service.impl;

import com.qls.core.constant.Constant;
import com.qls.core.dto.PushHotelBody;
import com.qls.core.entity.DtStore;
import com.qls.core.entity.OpGroup;
import com.qls.core.entity.OpTaskStatus;
import com.qls.rabbitmq.SendMsg;
import com.qls.redis.pojo.PullType;
import com.qls.web.modules.dt.service.StoreService;
import com.qls.web.modules.op.service.GroupService;
import com.qls.web.modules.op.service.HtlGroupService;
import com.qls.web.modules.op.service.PushGroupHotelService;
import com.qls.web.modules.op.service.TaskStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author doyoung
 */
@Service
public class PushGroupHotelServiceImpl implements PushGroupHotelService {

    @Autowired
    private HtlGroupService htlGroupService;

    @Autowired
    private StoreService dtStoreService;

    @Autowired
    private TaskStatusService opTaskStatusService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private SendMsg sendMsg;

    @Override
    public void pushGroup(Integer groupId) {
        List<Long> hotelIds = htlGroupService.getHotelIdByGroupId(groupId);
        DtStore dtStore = dtStoreService.getByGroupId(Integer.valueOf(groupId.toString()));
        OpGroup opGroup = groupService.getById(groupId);
        String name = opGroup.getGroupName();
        Integer storeId = dtStore.getId();
        Integer count = hotelIds.size();
        hotelIds.add(-1L);
        //创建任务
        OpTaskStatus opTaskStatus = new OpTaskStatus();
        opTaskStatus.setTaskName(String.format("分组[%s]酒店推送", name));
        opTaskStatus.setTotal(count);
        opTaskStatus.setStartTime(new Date());
        Integer taskCode = Integer.parseInt("" + System.currentTimeMillis() / 1000);
        opTaskStatus.setTaskCode(taskCode);
        opTaskStatus.setDtId(dtStore.getDtId());
        opTaskStatusService.save(opTaskStatus);
        for (Long hotelId : hotelIds) {
            PushHotelBody pushHotelBody = new PushHotelBody();
            pushHotelBody.setHotelId(hotelId);
            pushHotelBody.setTaskId(taskCode);
            pushHotelBody.setStoreId(storeId);
            pushHotelBody.setType(1);
            //将消息发送到“storeId_hotel”队列中
            sendMsg.execute(Constant.RabbitMq.HOTEL, storeId, pushHotelBody);
        }
    }
}
